package com.ease.common.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 
 * @author nathanleewei
 * 
 */
public class ExceptionUtil {

	public static void write(Throwable throwable, StringWriter writer) {
		if (null != throwable) {
			PrintWriter printWriter = new PrintWriter(writer);
			throwable.printStackTrace(printWriter);

			printWriter.close();
			printWriter = null;
		}

		// ByteArrayOutputStream out = new ByteArrayOutputStream();
		// try {
		// e.printStackTrace(new PrintStream(out));
		// } catch (Exception ex) {
		// ex.printStackTrace();
		// }finally{
		// try {
		// out.close();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		// }
		//
		// return new String(out.toByteArray());
	}

	public static String write(Throwable throwable) {
		if (null != throwable) {
			StringWriter writer = new StringWriter();
			write(throwable, writer);
			String stackTrace = writer.toString();
			try {
				if (null != writer) {
					writer.close();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				writer = null;
			}
			return stackTrace;
		}
		return null;

	}

	public static String toMessage(Throwable throwable) {
		if (null != throwable) {
			return throwable.getMessage() + "\n" + throwable.getClass() + "\n";
		}
		return null;
	}

}
